<?php
include("../contents.php");
include("../security.php");
include("../settings.php");
include("../database.php");
include("../statistics.php");
include("../xml.php");
$contents=new Contents(1);
if ($contents->checkedin==1) {
	$doc=new XML(0);
} else {
	$doc=new XML(1, 0);
}
if (!empty($_GET["editrepetition"]) && !empty($_GET["editblock"])) {
	$queryfield=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_blocks WHERE repetition='". $_GET["editrepetition"] ."' and name='". $_GET["editblock"] ."' and deleted='0'");
	if (mysql_num_rows($queryfield)<1) {
		mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."content_blocks SET repetition='". $_GET["editrepetition"] ."', name='". $_GET["editblock"] ."', content='', created_by='". $_SESSION["userid"] ."', created_date='". idate("U") ."'");
	}
}

if (isset($_POST["rte1"])) {
	$inhalt=utf8_decode(trim(rteSafe($_POST["rte1"])));
	$inhalt=stripslashes($inhalt);
	$inhalt=preg_replace("/<span style=\"font-weight: bold;\">(.*)<\/span>/U", "<strong>\$1</strong>", $inhalt);
	$inhalt=preg_replace("/<span style=\"font-style: italic;\">(.*)<\/span>/U", "<em>\$1</em>", $inhalt);
	$inhalt=$inhalt;
	mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_blocks SET deleted='1', delete_by='". $_SESSION["userid"] ."', delete_date='". idate("U") ."' WHERE repetition='". $_POST["edittextblockrepetition"] ."' and name='". $_POST["edittextblock"] ."'");
	mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."content_blocks SET repetition='". $_POST["edittextblockrepetition"] ."', name='". $_POST["edittextblock"] ."', content='". $inhalt ."', created_by='". $_SESSION["userid"] ."', created_date='". idate("U") ."'");
	forward_self();
}
if(!empty($_GET["reloadopener"])) {
	$doc->add_node("reloadopener", "1");
}
$doc->add_node("presence", $contents->actpresence);
$doc->array2xml($contents->get_documents(),0);
$doc->navarray2xml($contents->get_navigation());
// $doc->getblocktemplates();
$doc->output("globals/editfield.xsl");

function forward_self() {
	$zusatz="";
	if (!empty($_GET["name"])) {
		$zusatz.="&name=". $_GET["name"];
	}
	if (!empty($_GET["dirname"])) {
		$zusatz.="&dirname=". $_GET["dirname"];
	}
	if (!empty($_POST["edittextblockrepetition"])) {
		$zusatz.="&editrepetition=". $_POST["edittextblockrepetition"];
	}
	if (!empty($_POST["edittextblock"])) {
		$zusatz.="&editblock=". $_POST["edittextblock"];
	}
	header("Location:". $_SERVER["PHP_SELF"] ."?reloadopener=1". $zusatz);
	exit();
}
function rteSafe($strText) {
	//returns safe code for preloading in the RTE
	$tmpString = $strText;
	
	//convert all types of single quotes
	$tmpString = str_replace(chr(145), chr(39), $tmpString);
	$tmpString = str_replace(chr(146), chr(39), $tmpString);
	$tmpString = str_replace("'", "&#39;", $tmpString);
	
	//convert all types of double quotes
	$tmpString = str_replace(chr(147), chr(34), $tmpString);
	$tmpString = str_replace(chr(148), chr(34), $tmpString);
//	$tmpString = str_replace("\"", "\"", $tmpString);
	
	//replace carriage returns & line feeds
	$tmpString = str_replace(chr(10), " ", $tmpString);
	$tmpString = str_replace(chr(13), " ", $tmpString);
	$tmpString = str_replace("&gt;", ">", $tmpString);
	$tmpString = str_replace("&lt;", "<", $tmpString);
	$tmpString = str_replace("&amp;", "&", $tmpString);
	return $tmpString;
}
?>